package com.finalab.website.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.TransferManager;
import com.qcloud.cos.transfer.Upload;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class CosClientUtil {

     private static final String APPID = "1253457087";
     private static final String SECRETID = "AKIDVor78shZXO1OWOaMSIsBE1YptkmYJKNA";
     private static final String SECRETKEY = "uZzB8qOhla9TYNvFJ8h28yax4xe7aKn3";
     private static final String REGION = "ap-guangzhou";
     private static final String BUCKET = "finalweb-1253457087";
     private static final COSClient COSCLIENT;

     static {
         COSCredentials cred = new BasicCOSCredentials(SECRETID,SECRETKEY);
         ClientConfig clientConfig = new ClientConfig(new Region(REGION));
         COSCLIENT = new COSClient(cred, clientConfig);
     }

    /**
     * 生成下载链接
     */



    public static String GenerateSimplePresignedDownloadUrl(String key) {
        GeneratePresignedUrlRequest req =
                new GeneratePresignedUrlRequest(BUCKET, key, HttpMethodName.GET);
        // 设置签名过期时间(可选), 若未进行设置则默认使用ClientConfig中的签名过期时间(5分钟)
        // 这里设置签名在半个小时后过期
        Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
        req.setExpiration(expirationDate);
        URL url = COSCLIENT.generatePresignedUrl(req);
        return url.toString();
    }


    public static void uploadFile(String key, MultipartFile multipartFile){
        COSCredentials cred = new BasicCOSCredentials(SECRETID,SECRETKEY);
        ClientConfig clientConfig = new ClientConfig(new Region(REGION));
        COSClient cosclient = new COSClient(cred, clientConfig);
        ExecutorService threadPool = Executors.newFixedThreadPool(32);
        TransferManager transferManager = new TransferManager(cosclient, threadPool);
        String fileName = multipartFile.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        File tempFile = null;
        try {
            tempFile = File.createTempFile(key,prefix);
            multipartFile.transferTo(tempFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET, key, tempFile);
        try {
            Upload upload = transferManager.upload(putObjectRequest);
            UploadResult uploadResult = upload.waitForUploadResult();
        } catch (CosServiceException e) {
            e.printStackTrace();
        } catch (CosClientException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            if(tempFile != null){
                tempFile.delete();
            }
            transferManager.shutdownNow();
            cosclient.shutdown();
        }
    }


    public static void deleteFile(String key,String buket){
        COSCLIENT.deleteObject(buket, key);
        COSCLIENT.shutdown();
    }






}
